package andy.ham;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.Date;

import andy.ham.DaoMaster.DevOpenHelper;
import cc.trity.floatingactionbutton.FloatingActionButton;

public class DiaryEditor extends Activity {


	private static final String TAG = "Diary";
	public static final String EDIT_DIARY_ACTION = "andy.ham.DiaryEditor.EDIT_DIARY";
	public static final String INSERT_DIARY_ACTION = "andy.ham.DiaryEditor.action.INSERT_DIARY";

	// GreenDAO使用的变量
	private SQLiteDatabase db;
	private DaoMaster daoMaster;
	private DaoSession daoSession;
	private NoteDao noteDao;
	private Note note;


	private static final int STATE_INSERT = 1;

	private int mState;
	private EditText mTitleText;
	private EditText mBodyText;
	private long mid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(android.R.style.Theme_Black);
		final Intent intent = getIntent();
		final String action = intent.getAction();
		setContentView(R.layout.diary_edit);

		initDAO();
		Log.d("action", action);

		//删除按钮
		FloatingActionButton fabdel=(FloatingActionButton)findViewById(R.id.del);
		fabdel.setOnClickListener(new View.OnClickListener()
								{
									public void onClick(View v) {
										noteDao.deleteByKey(mid);
										Toast.makeText(DiaryEditor.this,"删除成功",
												Toast.LENGTH_LONG).show();
										Intent intent = new Intent(DiaryEditor.this, MyDiary.class);
										startActivity(intent);
										DiaryEditor.this.finish();
									}
								});

		//撤销按钮
		FloatingActionButton fabback=(FloatingActionButton)findViewById(R.id.back);
		fabback.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v) {
				Intent intent = new Intent(DiaryEditor.this, MyDiary.class);
				startActivity(intent);
				DiaryEditor.this.finish();
			}
		});

		//保存按钮
		FloatingActionButton fabsave=(FloatingActionButton)findViewById(R.id.save);
		fabsave.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v) {
				if (mState == STATE_INSERT) {
					insertDiary();
					Toast.makeText(DiaryEditor.this,"新建成功",
							Toast.LENGTH_LONG).show();
				} else {
					updateDiary();
					Toast.makeText(DiaryEditor.this,"编辑成功",
							Toast.LENGTH_LONG).show();
				}
				Intent intent = new Intent(DiaryEditor.this, MyDiary.class);
				startActivity(intent);
				DiaryEditor.this.finish();
			}
		});

		//分享按钮
		FloatingActionButton fabshare=(FloatingActionButton)findViewById(R.id.share);
		fabshare.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v) {
				String title = mTitleText.getText().toString();
				String body = mBodyText.getText().toString();
				Intent shareIntent = new Intent();
				shareIntent.setAction(Intent.ACTION_SEND);
				shareIntent.putExtra(Intent.EXTRA_TEXT,body);
				shareIntent.setType("text/plain");
				startActivity(Intent.createChooser(shareIntent,title));
			}
		});

		//标题及内容变量
		mTitleText = (EditText) findViewById(R.id.title);
		mBodyText = (EditText) findViewById(R.id.body);


		if (EDIT_DIARY_ACTION.equals(action)) {// 编辑日记
			Bundle bundle = new Bundle();
			bundle = this.getIntent().getExtras();
			mid = bundle.getLong("id");
			note = noteDao.loadByRowId(mid);
			mTitleText.setTextKeepState(note.getTitle());
			mBodyText.setTextKeepState(note.getBody());
			setTitle("编辑日记");
		} else if (INSERT_DIARY_ACTION.equals(action)) {// 新建日记
			mState = STATE_INSERT;
			setTitle("新建日记");
		} else {
			Log.e(TAG, "no such action error");
			finish();
			return;
		}
	}

	private void initDAO() {
		DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",
				null);
		db = helper.getWritableDatabase();
		daoMaster = new DaoMaster(db);
		daoSession = daoMaster.newSession();
		noteDao = daoSession.getNoteDao();
	}

	private void insertDiary() {
		String title = mTitleText.getText().toString();
		String body = mBodyText.getText().toString();
		final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
				DateFormat.MEDIUM);
		String date = df.format(new Date());
		Note note = new Note(null, title, body, date);
		noteDao.insert(note);
		Log.d("DaoExample", "Inserted new note, ID: " + note.getId());

	}

	private void updateDiary() {
		String title = mTitleText.getText().toString();
		String body = mBodyText.getText().toString();
		final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
				DateFormat.MEDIUM);
		String date = df.format(new Date());
		Note note = new Note(mid, title, body, date);
		noteDao.insertOrReplace(note);
	}
}
